<template>
    <div>
        <Title :title="title"></Title>
        <div ref="right1" style="height: 120px; width: 100%;"></div>
    </div>
</template>

<script>
import { DualAxes } from '@antv/g2plot';
import { get } from '@/utils/request';
import moment from 'moment';
import Title from './Title';

export default {
    data() {
        return {
            title: '实时湿度曲线',
            humData: []
        }
    },
    methods: {
        loadHum() {
           

            const dualAxes = new DualAxes(this.$refs.right1, {
                data:[this.humData,this.humData],
                xField: 'insert_time',
                yField: ['temperature', 'humidity'],
                   //配置x轴字段
                xAxis: {
                    label: {
                        rotate: -45,
                        style: {
                            fill: "white"
                        }
                    }
                },

                yAxis: [
                    {
                        label: {
                            style: {
                                fill: "white" // 第一个Y轴（temperature）文本白色
                            }
                        }
                    },
                    {
                        label: {
                            style: {
                                fill: "white" // 第二个Y轴（humidity）文本白色
                            }
                        }
                    }
                ],
                // 修正：图例文本样式（temperature/humidity显示名称）
                legend: {
                    position: 'top', // 确保图例位置正确
                    itemName: {
                        style: {
                            fill: 'white', // 图例文本颜色
                            fontSize: 14,  // 可选：设置字体大小以便查看
                        }
                    },
                    // 如果上面的配置不生效，尝试使用textStyle替代itemName
                    textStyle: {
                        fill: 'white',
                    }
                },
                // 数据点标签样式
                label: {
                    style: {
                        fill: "white"
                    }
                },
                // 动画配置
                animation: {
                    appear: {
                        animation: 'wave-in',
                        duration: 5000,
                    },
                },
                point: {
                    size: 5,
                    shape: 'diamond',
                    style: {
                        fill: 'white',
                        stroke: '#5B8FF9',
                        lineWidth: 2,
                    },
                },
                tooltip: {
                    showMarkers: false,
                    domStyles: {
                        "g2-tooltip": {
                            background: "#147",
                            color: "white",
                            fontSize: "20px"
                        }
                    }
                },
                legend: {
                    textStyle: {
                        fill: 'white'
                    }
                },
                geometryOptions: [
                    {
                        geometry: 'line',
                        color: '#5B8FF9',
                    },
                    {
                        geometry: 'line',
                        color: '#5AD8A6',
                    },
                ],
            });

            dualAxes.render();

        },
        async getHumData(){
            let res = await get('/dashboard/pageQueryTodayData',{page:1,pageSize:100})
            console.log(res,'获取相应')
            let temp = [];
            res.data.list.forEach((item,index,arr) =>{
                arr[0].temperature = 15.4;
                arr[0].humidity = 10.2;
                item.insert_time = moment(item.insert_time).format('HH:mm');         
                if(index % 10 == 0){
                    temp.push(arr[index])
                }
              });
              this.humData = temp;
        }
    },
    async mounted(){
        await this.getHumData();
        this.loadHum();

    }
};
</script>

<style scoped></style>